package com.crm.service.impl.cluePoolManager;

import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.cluePoolManager.CluePoolCompanyMapper;
import com.crm.dao.mapper.customMapper.cluePoolManagerCustomMapper.CluePoolCompanyCustomMapper;
import com.crm.dao.mapper.customMapper.cluePoolManagerCustomMapper.CluePoolCustomMapper;
import com.crm.model.entity.cluePoolManager.CluePoolCompany;
import com.crm.model.entity.cluePoolManager.CluePoolCompanyExample;
import com.crm.service.cluePoolManager.CluePoolCompanyDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Author yincuimei
 * @Date 2020/12/24 11:13
 */
@Service
public class CluePoolCompanyDbServiceImpl implements CluePoolCompanyDbService {

    @Autowired
    private CluePoolCompanyMapper cluePoolCompanyMapper;

    @Autowired
    private CluePoolCompanyCustomMapper cluePoolCompanyCustomMapper;



    @Override
    public CluePoolCompany selectByCluePoolIdAndCompanyIdAndUserPeopleId(String cluePoolId, String companyId, String userPeopleId, String permissionType) {
        CluePoolCompanyExample cluePoolCompanyExample = new CluePoolCompanyExample();
        CluePoolCompanyExample.Criteria criteria = cluePoolCompanyExample.createCriteria();
        criteria.andCluePollIdEqualTo(cluePoolId);
        criteria.andCompanyIdEqualTo(companyId);
        criteria.andUsePeopleIdEqualTo(userPeopleId);
        criteria.andPermissionTypeEqualTo(permissionType);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CluePoolCompany> cluePoolCompanies = cluePoolCompanyMapper.selectByExample(cluePoolCompanyExample);
        if(cluePoolCompanies.size()>0){
            return cluePoolCompanies.get(0);
        }else {
            return null;
        }
    }


    @Override
    public List<CluePoolCompany> selectByExample(CluePoolCompanyExample cluePoolCompanyExample) {
        return cluePoolCompanyMapper.selectByExample(cluePoolCompanyExample);
    }

    @Override
    public Boolean insertCluePoolCompany(CluePoolCompany cluePoolCompany) {
        Boolean result = false;
        int num = cluePoolCompanyMapper.insertSelective(cluePoolCompany);
        if(num>0){
            result = true;
        }
        return result;
    }

    @Override
    public Boolean updatePoolCompany(String poolId,String permissionType) {
        CluePoolCompany cluePoolCompany = new CluePoolCompany();
        cluePoolCompany.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.DELETE_CODE.getState())));

        CluePoolCompanyExample cluePoolCompanyExample = new CluePoolCompanyExample();
        CluePoolCompanyExample.Criteria criteria = cluePoolCompanyExample.createCriteria();
        criteria.andCluePollIdEqualTo(poolId);
        criteria.andPermissionTypeEqualTo(permissionType);
        int num = cluePoolCompanyMapper.updateByExampleSelective(cluePoolCompany,cluePoolCompanyExample);
        return num > 0;
    }

    @Override
    public List<CluePoolCompany> selectByCluePoolIdAndPermissionType(String cluePoolId, String permissionType) {
        CluePoolCompanyExample cluePoolCompanyExample = new CluePoolCompanyExample();
        CluePoolCompanyExample.Criteria criteria = cluePoolCompanyExample.createCriteria();
        criteria.andCluePollIdEqualTo(cluePoolId);
        criteria.andPermissionTypeEqualTo(permissionType);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<CluePoolCompany> cluePoolCompanies = cluePoolCompanyMapper.selectByExample(cluePoolCompanyExample);
        return cluePoolCompanies;
    }

    /**
     * 私有池查询使用组人员
     * @param cluePollId   ---线索池主键
     * @param deleteStatus ---删除状态
     * @return
     */
    @Override
    public List<Map<String, Object>> selectPersonByDistributePermission(String cluePollId, Short deleteStatus) {
        return cluePoolCompanyCustomMapper.selectPersonByDistributePermission(cluePollId,deleteStatus);
    }

    /**
     * 私有池查询使用组公司
     * @param cluePollId   ---线索池主键
     * @param deleteStatus ---删除状态
     * @return
     */
    @Override
    public List<Map<String, Object>> selectCompanyByDistributePermission(String cluePollId, Short deleteStatus) {
        return cluePoolCompanyCustomMapper.selectCompanyByDistributePermission(cluePollId,deleteStatus);
    }
}
